/*
 * @(#)BikeTrip.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package Example.Components;

import Example.Interfaces.BQuery;
import Example.Interfaces.BSave;
import Example.Interfaces.BikeOprs;
import Example.Interfaces.CourseOprs;
import Example.Interfaces.LocOprs;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import java.util.Vector;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/12/30
 * @author         Huaxi (Yulin) ZHANG    
 */
public class BikeTrip implements BikeOprs, CourseOprs, BindingController {

    /** Field description */
    private BQuery BQuery;

    /** Field description */
    private BSave BSave;

    /** Field description */
    private LocOprs LocOprs;

    /**
     * Method description
     *
     * @param locationStart  
     * @param locationEnd  
     * @return  
     */
    public Vector selectBike(String locationStart, String locationEnd) {

        // TODO Auto-generated method stub
        Vector bikes    = new Vector();
        double distance = 0.0;

        if (LocOprs != null) {
            String station = LocOprs.findLoc(locationStart);

            if (station != null) {
                bikes = BQuery.findBike(station);
            } else {
                bikes = BQuery.findBike(locationStart);
            }

            distance = LocOprs.calcDistance(locationStart, locationEnd);
        } else {
            bikes = BQuery.findBike(locationStart);
        }

        bikes.add(Double.toString(distance));

        return bikes;
    }

    /**
     * Method description
     *
     * @param bikeID  
     * @param location  
     * @param trip  
     * @return  
     */
    public boolean returnBike(int bikeID, String location, double trip) {
        BSave.saveBike(bikeID, location, trip);

        return false;
    }

    /**
     * Method description
     *
     * @param bikeID  
     * @param userID  
     * @return  
     */
    public boolean rentBike(int bikeID, int userID) {
        boolean b;
        String  c;

        b = BSave.saveBike(bikeID, "", 0);
        c = BSave.saveCourse(userID, bikeID);

        if (b) {
            return true;
        }

        return false;
    }

    /**
     * Method description
     *
     * @param userID  
     * @param bikeID  
     * @return  
     */
    public String endCourse(int userID, int bikeID) {
        return BSave.saveCourse(userID, bikeID);
    }

    /**
     * Method description
     *
     * @param userID  
     * @return  
     */
    public Object[] startCourse(int userID) {

        // TODO Auto-generated method stub
        return BQuery.findCourse(userID);
    }

    /**
     * Method description
     *
     * @return  
     */
    public String[] listFc() {
        return new String[] { "BQuery", "BSave", "LocOprs" };
    }

    /**
     * Method description
     *
     * @param cItf  
     * @return  
     * @throws NoSuchInterfaceException  
     */
    public Object lookupFc(String cItf) throws NoSuchInterfaceException {
        if (cItf.equals("BQuery")) {
            return BQuery;
        }

        if (cItf.equals("BSave")) {
            return BSave;
        }

        if (cItf.equals("LocOprs")) {
            return LocOprs;
        }

        return null;
    }

    /**
     * Method description
     *
     * @param cItf  
     * @throws IllegalBindingException  
     * @throws IllegalLifeCycleException  
     * @throws NoSuchInterfaceException  
     */
    public void unbindFc(String cItf)
            throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
        if (cItf.equals("BQuery")) {
            BQuery = null;
        }

        if (cItf.equals("BSave")) {
            BSave = null;
        }

        if (cItf.equals("LocOprs")) {
            LocOprs = null;
        }
    }

    /**
     * Method description
     *
     * @param cItf  
     * @param sItf  
     * @throws IllegalBindingException  
     * @throws IllegalLifeCycleException  
     * @throws NoSuchInterfaceException  
     */
    public void bindFc(String cItf, Object sItf)
            throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
        if (cItf.equals("BQuery")) {
            BQuery = (BQuery) sItf;
        }

        if (cItf.equals("BSave")) {
            BSave = (BSave) sItf;
        }

        if (cItf.equals("LocOprs")) {
            LocOprs = (LocOprs) sItf;
        }
    }
}
